package com.tpr.controller;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpr.entity.User;
import com.tpr.service.UserService;

@Controller
@RequestMapping
public class UserController {
	//byType方式，配置好的bean的
	@Autowired
	private UserService userService;
/**	
	@RequestMapping("user")
	@ResponseBody
	public List<User> getAllUser(HttpServletRequest req){
		
		List<User> user=userService.getAllUsers();
		
		return user;
		
	}
	*/
	@RequestMapping("user")
	@ResponseBody
	public PageInfo<User> getAllUser(HttpServletRequest req){
		int curNum = Integer.parseInt(req.getParameter("curNum"));
		System.out.println("curNum:"+curNum);
		PageHelper.startPage(curNum,5);
		List<User> user=userService.getAllUsers();
		PageInfo<User> pageInfo =new PageInfo<User>(user);
		//pageInfo.setPageNum(curNum);
		return pageInfo;
	}
	
	@RequestMapping("delUser")
	//@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中
	//，一般在异步获取数据时使用【也就是AJAX】，
	//在使用 @RequestMapping后，返回值通常解析为跳转路径，
	//但是加上 @ResponseBody 后返回结果不会被解析为跳转路径，
	//而是直接写入 HTTP response body 中。 比如异步获取 json 数据，加上 @ResponseBody 后，会直接返回 json 数据。
	@ResponseBody
	public int delUser(HttpServletRequest req) {
		int id=Integer.parseInt(req.getParameter("id"));
		int res=userService.delUser(id);
		System.out.println(res);
		return res;
	}
	
	@RequestMapping("add")
	public String insert(HttpServletRequest req) {
		User user = new User();
		try {
		String name =req.getParameter("name");
		int age = Integer.parseInt(req.getParameter("age"));
		String sex = req.getParameter("sex");
		String tel = req.getParameter("tel");
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
		Date birthday = sdf.parse(req.getParameter("birthday"));
		java.sql.Date sqlDate = new java.sql.Date(birthday.getTime());
		String tip = req.getParameter("tip");
		
		user.setName(name);
		user.setAge(age);
		user.setBirthday(sqlDate);
		user.setSex(sex);
		user.setTel(tel);
		user.setTip(tip);
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int res = userService.insertUser(user);
		System.out.println(res);
		if(res==1) {
			return "redirect:http://localhost:8020/Mine/index.html";
		}
		return "";
	}
	
	@RequestMapping("updateUser")
	public String updateUser(HttpServletRequest req) {
		System.out.println("updateUser");
		User user=new User();
		try {
		int id=Integer.parseInt(req.getParameter("id"));
		String name=req.getParameter("name");
		int age=Integer.parseInt(req.getParameter("age"));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date birthday=sdf.parse(req.getParameter("birthday"));
		java.sql.Date sqlDate=new java.sql.Date(birthday.getTime());
		String sex=req.getParameter("sex");
		String tel=req.getParameter("tel");
		String tip=req.getParameter("tip");
		user.setAge(age);
		user.setBirthday(sqlDate);
		user.setId(id);
		user.setName(name);
		user.setSex(sex);
		user.setTel(tel);
		user.setTip(tip);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int res=userService.updateUser(user);
		if(res==1) {
			return "redirect:http://localhost:8020/Mine/index.html";
		}
		return "";
	}
	
	

}
